<template>
  <div class="operateActivitystyle">
    <!-- 标题 -->
    <h2>{{ datas.text }}</h2>
    <!-- 内容设置 -->
    <el-form label-width="80px" :model="datas" size="small" :rules="rules">
      <el-form-item class="lef" label="选择样式">
        <p style="color: #000; line-height: 4px">{{ styleText }}</p>
      </el-form-item>
      <!-- 样式选择 -->
      <div class="commodityType">
        <el-tooltip
          v-for="(item, index) in commodityTypes"
          :key="index"
          class="item"
          effect="dark"
          :content="item.content"
          placement="bottom">
          <span
            class="iconfont"
            style="font-size: 21px"
            :class="[item.type === datas.commodityType ? 'active' : '', item.icon]"
            @click="onTest(index)">
          </span>
        </el-tooltip>
      </div>
      <!-- 标题设置 -->
      <div class="titlesetting">
        <el-form-item label="标题图片">
          <div class="backgroundImg">
            <img
              v-if="!datas.titlePic"
              draggable="false"
              src="../../../assets/images/add.png"
              alt=""
              @click="showUpload('first')" />
            <div v-else>
              <img draggable="false" :src="datas.titlePic" alt="" />
              <p @click="deleteTitpic">删除</p>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="标题名称">
          <el-input v-model="datas.title" placeholder="请输入标题" show-word-limit clearable />
        </el-form-item>
        <el-form-item label="活动标签">
          <el-input v-model="datas.activeFlag" placeholder="请输入活动标签" show-word-limit clearable />
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="datas.activeEndTime"
            type="datetime"
            @change="endtimeChange"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始颜色" prop="backgroundStartColor" class="lef">
          <el-color-picker v-model="datas.backgroundStartColor" @change="firstChangeColor" class="picke" :predefine="predefineColors" show-alpha />
        </el-form-item>
        <el-form-item label="结束颜色" prop="backgroundEndColor" class="lef">
          <el-color-picker v-model="datas.backgroundEndColor" @change="secondChangeColor" class="picke" :predefine="predefineColors" show-alpha />
        </el-form-item>
        <el-form-item label="标签颜色" prop="activeTimeColor" class="lef">
          <el-color-picker v-model="datas.activeTimeColor" class="picke" :predefine="predefineColors" show-alpha />
        </el-form-item>
      </div>
      <!-- 添加内容 -->
      <div class="addcontentlist">
        <vuedraggable v-model="datas.imageList" v-bind="dragOptions">
          <transition-group>
            <section v-for="(item, index) in datas.imageList" :key="item + index" class="imgBanner">
              <i class="el-icon-circle-close" @click="deleteimg(index)" />
              <!-- 图片 -->
              <div class="imag">
                <img
                  src="../../../assets/images/add.png"
                  @click="showUpload('second', index)"
                  alt
                  draggable="false"
                  v-if="!item.src" />
                <img :src="item.src" @click="showUpload('second', index)" alt="" draggable="false"/>
              </div>
              <!-- 标题和链接 -->
              <div class="imgText">
                <el-input v-model="item.text" placeholder="标题" size="mini" clearable/>
                <div style="height: 5px" />
                <el-input v-model="item.desc" placeholder="描述" size="mini" clearable/>
                <div style="height: 5px" />
                <!-- 标题和链接 -->
                <div class="imgTextChild">
                  <!-- 选择类型 -->
                  <select-link @selectThisLink="selectThisLink" @clearThisLink="clearThisLink(index)" :linktypeurl="item.url" :theIndex="index" />
                </div>
                <div style="height: 5px" />
                <!-- 场景 -->
                <el-input v-show="datas.commodityType === 0" v-model="item.studyStatus" placeholder="场景" size="mini" clearable />
                <div style="height: 5px" />
                <!-- 运营标签 -->
                <el-select
                  v-model="item.flagTagsArr"
                  clearable
                  multiple
                  filterable
                  remote
                  reserve-keyword
                  allow-create
                  default-first-option
                  placeholder="输入运营标签,按回车添加"
                  @change="changeValue($event,index)">
                  <el-option v-for="item in flagTags" :key="item.value" :label="item.label" :value="item.label"></el-option>
                </el-select>
                <div style="height: 5px" />
                <!-- 按钮文字 -->
                <el-input v-model="item.btnText" placeholder="按钮文字" size="mini" clearable/>
                <el-form-item class="lef" label="VIP标识">
                  <el-radio-group v-model="item.isVipShow">
                    <el-radio :label="2">隐藏</el-radio>
                    <el-radio :label="1">显示</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item class="lef" label="价格展示">
                  <el-radio-group v-model="item.showPrice" @change="radioChange($event,index)">
                    <el-radio :label="2">隐藏</el-radio>
                    <el-radio :label="1">显示</el-radio>
                  </el-radio-group>
                </el-form-item>
              </div>
            </section>
          </transition-group>
        </vuedraggable>
      </div>
      <el-button class="uploadImg" type="primary" plain @click="addContent" >
        <i class="el-icon-plus" />
        点击添加
      </el-button>
    </el-form>
    
    <!-- 上传图片 -->
    <uploadimg ref="upload" @uploadInformation="uploadInformation" />
  </div>
</template>
<script>
import vuedraggable from "vuedraggable"; // 拖拽组件
import uploadimg from "../../uploadImg"; // 图片上传
import SelectLink from "../../selectLink";
export default {
  name: "OperateActivitystyle",
  components: {
    vuedraggable,
    uploadimg,
    SelectLink,
  },
  props: {
    datas: Object,
  },
  data() {
    const kon = (rule, value, callback) => {
      if (value.length === 0) callback(new Error("请输入有效数字"));
    };
    return {
      commodityTypes: [
        {
          icon: "icon-xiaotuhengxianghuadong",
          type: 0,
          content: "一行两个半"
        },
        {
          icon: "icon-yihangsange",
          type: 1,
          content: "一行三个"
        }
      ],
      rules: {
        textWeight: [{ required: true, validator: kon, trigger: "blur" }],
      },
      dragOptions: {
        animation: 200,
      },
      predefineColors: [
        // 颜色选择器预设
        "#ff4500",
        "#ff8c00",
        "#ffd700",
        "#90ee90",
        "#00ced1",
        "#1e90ff",
        "#c71585",
        "#409EFF",
        "#909399",
        "#C0C4CC",
        "rgba(255, 69, 0, 0.68)",
        "rgb(255, 120, 0)",
        "hsv(51, 100, 98)",
        "hsva(120, 40, 94, 0.5)",
        "hsl(181, 100%, 37%)",
        "hsla(209, 100%, 56%, 0.73)",
        "#c7158577"
      ],
      flagTags: []  // 存储标签的数组
    };
  },
  computed: {
    styleText() {
      if (this.datas.commodityType === 0) return "一行两个半";
      if (this.datas.commodityType === 1) return "一行三个";
    },
  },
  methods: {
    onTest (index) {
      this.datas.commodityType = index;
      this.$emit('updateView');
    },
    endtimeChange () {
      this.datas.activeEndTime = Date.parse(this.datas.activeEndTime);
    },
    changeValue (val,index) {
      const valstr = val.join(',');
      this.datas.imageList[index].flag = valstr;
    },
    radioChange (val,index) {
      if (val === 1) {
        this.datas.imageList[index].isShowPrice = true;
      } else {
        this.datas.imageList[index].isShowPrice = false;
      }
    },
    firstChangeColor(val) {
      this.datas.backgroundStartColor = val;
      if (Object.keys(this.datas.backgroundEndColor).length == 0) {
        this.$message({
          message: "请选择开始颜色",
          type: "warning",
        });
      } else {
        this.datas.bgcolors =  "linear-gradient(180deg," + this.datas.backgroundStartColor + '0%' + "," + this.datas.backgroundEndColor + '100%' + ")";
      }
    },
    secondChangeColor(val) {
      this.datas.backgroundEndColor = val;
      if (Object.keys(this.datas.backgroundStartColor).length == 0) {
        this.$message({
          message: "请选择结束颜色",
          type: "warning",
        });
      } else {
        this.datas.bgcolors =  "linear-gradient(180deg," + this.datas.backgroundStartColor + '0%' + "," + this.datas.backgroundEndColor + '100%' + ")";
      }
    },
    addContent() {
      this.datas.imageList.push({
        src: "",
        text: "这里显示标题最多显示2行",
        desc: "这里显示描述",
        type: "1",
        id: "",
        courseNum: 0,
        studyStatus: '',
        flag: '', // 运营标签
        flagTagsArr: [],
        isVipShow: 1,
        showPrice: 1,
        isShowPrice: true
      });
    },
    showUpload(type, index) {
      this.changeType = type;
      this.changeIndex = index;
      this.$refs.upload.showUpload();
    },
    // 提交
    uploadInformation(res) {
      if (this.changeType == "first") {
        this.datas.titlePic = res;
      } else {
        this.datas.imageList[this.changeIndex].src = res;
      }
    },
    // 选择链接
    selectThisLink(resu) {
      this.datas.imageList.forEach((it, ind) => {
        if (ind == resu.index) {
          it.url = resu.url;
          it.linktype = resu.linktype;
          if(resu.name != undefined){
            it.text = resu.name;
          }
          if(resu.note != undefined){
            it.desc = resu.note;
          }
          if(resu.fileUrlSmall != undefined){
            it.src = resu.fileUrlSmall;
          }
          it.id = resu.id;
          it.courseNum = resu.courseNum;
          it.type = resu.type;
          it.suitesId = resu.suitesId;
          it.goodsLabel = resu.goodsLabel;
        }
      });
    },
    clearThisLink (index) {
      this.datas.imageList[index].url = '';
    },
    /* 删除图片 */
    deleteimg(index) {
      this.datas.imageList.splice(index, 1);
    },
    // 删除标题图片
    deleteTitpic () {
      this.datas.titlePic = '';
    }
  }
};
</script>
<style lang="scss" scoped>
.operateActivitystyle {
  width: 100%;
  /* 标题 */
  h2 {
    padding: 24px 16px 24px 0;
    margin-bottom: 15px;
    border-bottom: 1px solid #f2f4f6;
    font-size: 18px;
    font-weight: 600;
    color: #323233;
  }
  .lef {
    .el-form-item__label {
      text-align: left;
    }
  }
  /* 列表样式 */
  .commodityType {
    display: flex;
    justify-content: space-around;
    align-items: center;
    span {
      display: inline-block;
      width: 58px;
      height: 32px;
      text-align: center;
      line-height: 32px;
      background: #fff;
      border: 1px solid #ebedf0;
      color: #979797;
      margin: 0 1px;
      cursor: pointer;
      transition: all 0.5s;
      &:hover {
        border: 1px solid #155bd4;
        color: #155bd4;
      }
      &.active {
        border: 1px solid #155bd4;
        background-color: #e0edff;
        color: #155bd4;
      }
    }
  }
  .titlesetting{
    margin-top: 30px;
    .backgroundImg {
      cursor: pointer;
      position: relative;
      width: 80px;
      height: 80px;
      img {
        width: 100%;
        height: auto;
        border: 1px solid #ccc;
      }
      p {
        position: absolute;
        left: 0;
        bottom: -15px;
        width: 100%;
        height: 20px;
        font-size: 12px;
        color: #fff;
        background: rgba(0, 0, 0, 0.5);
        text-align: center;
        line-height: 20px;
      }
    }
  }
  .imgBanner {
    padding: 6px 12px;
    margin: 16px 7px;
    border-radius: 2px;
    background-color: #fff;
    box-shadow: 0 0 4px 0 rgba(10, 42, 97, 0.2);
    display: flex;
    position: relative;
    /* 删除图标 */
    .el-icon-circle-close {
      position: absolute;
      right: -10px;
      top: -10px;
      cursor: pointer;
      font-size: 19px;
    }
    /* 图片 */
    .imag {
      width: 90px;
      height: 90px;
      border-radius: 5px;
      overflow: hidden;
      position: relative;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        display: inline-block;
      }
      span {
        background: rgba(0, 0, 0, 0.5);
        font-size: 12px;
        position: absolute;
        left: 0px;
        bottom: 0px;
        display: inline-block;
        width: 100%;
        text-align: center;
        color: #fff;
        height: 20px;
        line-height: 20px;
      }
    }
    /* 图片字 */
    .imgText {
      width: 80%;
      display: flex;
      flex-direction: column;
      box-sizing: border-box;
      padding-left: 10px;
      justify-content: space-between;
      /* 图片字 */
      .imgTextChild {
        width: 100%;
        display: flex;
        box-sizing: border-box;
        justify-content: space-between;
        .fir-sele.el-select {
          width: 40%;
        }
      }
    }
  }
  /* 上传图片按钮 */
  .uploadImg {
    width: 345px;
    height: 40px;
    margin-top: 20px;
  }
  // 上传弹框内容部分
  .uploadIMG .el-dialog__body {
    height: 280px;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    justify-content: center;
  }
}
</style>
